package cn.itcast.core.solr;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;

@ContextConfiguration(locations = {"classpath:applicationContext-solr.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class SolrDemo {

    @Resource
     private SolrTemplate solrTemplate;

    @Test
    public void testSaveOrUpdate(){
        Item item = new Item();
        item.setId(3L);
        item.setTitle("三星");
        solrTemplate.saveBean(item);
        solrTemplate.commit();
    }
    @Test
    public void testSelectById(){
        Item item = solrTemplate.getById(1,Item.class);
        System.out.println(item);
    }
    @Test
    public void  cha(){
        Criteria criteria = new Criteria("item_title");
        criteria = criteria.is("");
        SimpleQuery query = new SimpleQuery(criteria);
        ScoredPage<Item> items = solrTemplate.queryForPage(query, Item.class);
        List<Item> content = items.getContent();
        for (Item item : items) {
            System.out.println(item);
        }
    }

    @Test
    public void del(){
        solrTemplate.deleteById("1");
        solrTemplate.commit();
    }
}
